import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';

import '../../models/information.dart';
import '../../widgets/load_image_widget.dart';
import '../../widgets/banner_carousel_widget.dart';
import 'logic.dart';

class InformationPage extends StatelessWidget {
  InformationPage({Key? key}) : super(key: key);

  final InformationLogic logic = Get.put(InformationLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('乡村振兴信息'),
        backgroundColor: Colors.green,
        actions: [
          Obx(() => logic.isAdmin.value
              ? Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              IconButton(
                icon: Icon(Icons.image),
                onPressed: logic.goToManageBanners,
                tooltip: '管理轮播图',
              ),
              IconButton(
                icon: Icon(Icons.add),
                onPressed: logic.goToAddInformation,
                tooltip: '添加信息',
              ),
            ],
          )
              : SizedBox()),
        ],
      ),
      body: Obx(() {
        if (logic.isLoading.value) {
          return Center(child: CircularProgressIndicator());
        }

        return EasyRefresh(
          onRefresh: () async {
            await logic.loadData();
          },
          child: ListView.builder(
            padding: EdgeInsets.only(bottom: 16.w),
            itemCount: logic.informationList.isEmpty ? 2 : logic.informationList.length + 1,
            itemBuilder: (context, index) {
              if (index == 0) {
                // 轮播图部分
                return Padding(
                  padding: EdgeInsets.only(bottom: 16.w),
                  child: Obx(() {
                    if (logic.isBannersLoading.value) {
                      return Container(
                        height: 180.h,
                        color: Colors.grey[200],
                        child: Center(
                          child: CircularProgressIndicator(),
                        ),
                      );
                    }

                    return BannerCarouselWidget(
                      banners: logic.bannerList,
                      height: 180,
                    );
                  }),
                );
              } else if (logic.informationList.isEmpty && index == 1) {
                // 没有信息时的空状态
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.info_outline, size: 60, color: Colors.grey),
                      SizedBox(height: 16),
                      Text(
                        '暂无信息',
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                );
              } else {
                // 信息卡片
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.w),
                  child: _buildInformationCard(logic.informationList[index - 1]),
                );
              }
            },
          ),
        );
      }),
    );
  }

  Widget _buildInformationCard(Information information) {
    return Card(
      margin: EdgeInsets.only(bottom: 16.w),
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 图片部分
          if (information.imageUrl != null && information.imageUrl!.isNotEmpty)
            ClipRRect(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12.r),
                topRight: Radius.circular(12.r),
              ),
              child: LoadImageWidget(
                information.imageUrl!,
                width: double.infinity,
                height: 180.h,
              ),
            ),
          // 内容部分
          InkWell(
            onTap: () => logic.goToInformationDetail(information),
            child: Padding(
              padding: EdgeInsets.all(16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    information.title,
                    style: TextStyle(
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 8.h),
                  Text(
                    information.content,
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.grey[700],
                    ),
                    maxLines: 3,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 8.h),
                  Text(
                    '发布时间: ${information.publishDate}',
                    style: TextStyle(
                      fontSize: 12.sp,
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
          ),
          // 管理员操作按钮
          Obx(() => logic.isAdmin.value
              ? Padding(
            padding: EdgeInsets.only(right: 16.w, bottom: 16.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton.icon(
                  onPressed: () => logic.deleteInformation(information),
                  icon: Icon(Icons.delete, color: Colors.red),
                  label: Text('删除', style: TextStyle(color: Colors.red)),
                ),
              ],
            ),
          )
              : SizedBox()),
        ],
      ),
    );
  }
}